// 封装和作品相关的接口
import axios from 'axios';
import { getToken } from '../utils';

// 创建axios实例
const request = axios.create({
  baseURL: process.env.REACT_APP_API_URL || 'http://211.86.197.10:9091/api',
  timeout: 10000,
});

// 请求拦截器
request.interceptors.request.use(
  (config) => {
    const token = getToken();
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
request.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 1.获取频道列表
export function getChannelsAPI() {
  return request({
    url: '/channels',
    method: 'GET'
  });
}

// 2.提交作品表单
export function createArticleAPI(data) {
  return request({
    url: '/articles',
    method: 'POST',
    data
  });
}

// 3.获取作品列表
export function getArticlesAPI(params) {
  return request({
    url: '/articles',
    method: 'GET',
    params
  });
}

// 4.获取作品详情
export function getArticleAPI(id) {
  return request({
    url: `/articles/${id}`,
    method: 'GET'
  });
}

// 5.更新作品
export function updateArticleAPI(id, data) {
  return request({
    url: `/articles/${id}`,
    method: 'PUT',
    data
  });
}

// 6.删除作品
export function deleteArticleAPI(id) {
  return request({
    url: `/articles/${id}`,
    method: 'DELETE'
  });
}